<?php echo Html::script('media/js/jquery-1.9.1.js');?>
<script type="text/javascript">
$(document).ready(function(){
	$("#category").change(function(){
		var category_id = $("#category option:selected").attr("value");
		var subcategory_id = category_id;
		var url = <?php echo '"'.URL::base().'admin/promotions/new"' ?>;
		var form = document.createElement("form");
		form.setAttribute("method", "post");
		form.setAttribute("action", url);
		var categoryField = document.createElement("input");
		categoryField.setAttribute("type", "hidden");
		categoryField.setAttribute("name", "category_id");
		categoryField.setAttribute("value", category_id);
		form.appendChild(categoryField);
		var subcategoryField = document.createElement("input");
		subcategoryField.setAttribute("type", "hidden");
		subcategoryField.setAttribute("name", "subcategory_id");
		subcategoryField.setAttribute("value", subcategory_id);
		form.appendChild(subcategoryField);
		document.body.appendChild(form);
		form.submit();
		
		
	});
	$("#subcategory").change(function() {
		var category_id = $("#category option:selected").attr("value");
		var subcategory_id = $("#subcategory option:selected").attr("value");
		var url = <?php echo '"'.URL::base().'admin/promotions/new"' ?>;
		var form = document.createElement("form");
		form.setAttribute("method", "post");
		form.setAttribute("action", url);
		var categoryField = document.createElement("input");
		categoryField.setAttribute("type", "hidden");
		categoryField.setAttribute("name", "category_id");
		categoryField.setAttribute("value", category_id);
		form.appendChild(categoryField);
		var subcategoryField = document.createElement("input");
		subcategoryField.setAttribute("type", "hidden");
		subcategoryField.setAttribute("name", "subcategory_id");
		subcategoryField.setAttribute("value", subcategory_id);
		form.appendChild(subcategoryField);
		document.body.appendChild(form);
		form.submit();
		
	});
});
</script>


<select id="category" name="category">
<?php foreach($categories as $category): ?>
<?php if($category->id == $values['category_id']):?>
<option value="<?php echo $category->id;?>" selected><?php echo $category->name;?></option>
<?php else: ?>
<option value="<?php echo $category->id;?>"><?php echo $category->name;?></option>
<?php endif;?>
<?php endforeach;?>
</select>
<?php $cat = ORM::factory('category', $values['category_id']);?>

<select id="subcategory" name="subcategory_id">
<?php if($values['subcategory_id'] == $cat->id):?>
<option value="<?php echo $cat->id;?>" selected>All <?php echo $cat->name;?></option>
<?php else:?>
<option value="<?php echo $cat->id;?>">All <?php echo $cat->name;?></option>
<?php endif;?>

<?php foreach(ORM::factory('category', $values['category_id'])->subcategories->order_by('name', 'asc')->find_all() as $sub):?>
<?php if($sub->id == $values['subcategory_id']):?>
<option value="<?php echo $sub->id;?>" selected><?php echo $sub->name;?></option>
<?php else: ?>
<option value="<?php echo $sub->id;?>"><?php echo $sub->name;?></option>
<?php endif;?>
<?php endforeach;?>
</select>


<?php echo Html::anchor('admin/promotions/createforcategory/'.$values['subcategory_id'], 'Create Promotion for this Category');?>

<div id="items">
<?php echo $list; ?>
</div>

